﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum SpawnDirection
{
    Left = -1,
    Right = 1
}

public class MapCheckpoint : MonoBehaviour
{
    public SpawnDirection playerSpawnDirection = SpawnDirection.Right;

    public bool _isActivated = false;

    private Collider2D _collider2D;
    private SpriteRenderer _spriteRenderer;


    int isOnParamID; //ID of the isOn
    Animator anim; //Reference to the Animator component


    private void Start()
    {
        _collider2D = GetComponent<Collider2D>();
        _spriteRenderer = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();

        isOnParamID = Animator.StringToHash("isOn");
    }

    private void Update()
    {
        anim.SetBool(isOnParamID, _isActivated);
    }

    public void Spawn(GameObject player)
    {
        // 将玩家刚体速度重置
        var rigidbody2D = player.GetComponent<Rigidbody2D>();
        rigidbody2D.velocity = Vector2.zero;

        // 设置玩家位置
        player.transform.position = transform.position;

        // 设置玩家面向
        var playerMovement = player.GetComponent<PlayerMovement>();
        playerMovement.SetDirection(playerSpawnDirection == SpawnDirection.Right);
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        // 已经激活过不需要再激活
        if (_isActivated) return;


        if (other.CompareTag("Player"))
        {
            _isActivated = true;
            var playerController = other.GetComponent<PlayerCheckpoint>();
            playerController.checkPoint = this;

            _collider2D.enabled = false;

            Debug.Log("玩家激活了存档点");
        }
    }
}